<?php
function SignUp($username, $password,$first_name, $Last_name)
{
$con = mysql_connect("localhost","root","");
if(!$con)
{
    die('Could not connect:' . mysql_error());
}
mysql_select_db("db_name",$con);

   $result = mysql_query("INSERT INTO Members(username, password, first_name, last_name)
  VALUES ('$username','$password','$first_name','$Last_name')")
           or die(mysql_error());
   header("location:index.php");
   
 mysql_close($con);
}

function SignIn($username, $password)
{
$con = mysql_connect("localhost","root","");
if(!$con)
{
    die('Could not connect:' . mysql_error());
}
mysql_select_db("db_name",$con);
 
    $result = mysql_query("SELECT * FROM Members Where username='$username' AND password='$password';") or die(mysql_error());
    $count = mysql_num_rows($result);
    if($count == 1)
    {
        session_register("$username");
        session_register("$password");
        header("location:newfunctions.php");    
    }
    else
    {
        echo "wrong username or password";
    }
     mysql_close($con);
}

function Insert($text, $authorname)
        {
$con = mysql_connect("localhost","root","");
if(!$con)
{
    die('Could not connect:' . mysql_error());
}
mysql_select_db("db_name",$con);

mysql_query("INSERT INTO Texts(text, author_username , date_added)
          VALUES ('$text','$authorname','getDate()')") or die(mysql_error()); 
     mysql_close($con);     
        }
        
function ViewText()
        {
$con = mysql_connect("localhost","root","");
if(!$con)
{
    die('Could not connect:' . mysql_error());
}
mysql_select_db("db_name",$con);
           $result = mysql_query("SELECT * FROM Texts") or die(mysql_error()); 
           
           echo '<table width="100%">
                <tr>
                    <td><b>user</b></td>
                    <td><b>says</b></td>
                    <td><b>Created</b></td>
                    
                </tr>';
        // Print each file
        while($row = mysql_fetch_assoc($result)) {
            echo "
                <tr>
                    <td>{$row['author_username']}</td>
                    <td>{$row['text']}</td>
                    <td>{$row['date_added']}</td>
                    </tr>"; 
        }
        // Close table
        echo '</table>';
   

      mysql_close($con);      
        }
        

        
Function SearchByAuthor($authorNeeded)
        {
$con = mysql_connect("localhost","root","");
if(!$con)
{
    die('Could not connect:' . mysql_error());
}
mysql_select_db("db_name",$con);

        $result = mysql_query("Select * 
        From Texts t inner join Members m 
        on  t.author_username = m.username 
        where t.author_username = '$authorNeeded';") or die(mysql_error());

While($row = mysql_fetch_assoc($result))
 
   echo $row['author_username'] . "  wrote  " . $row['text'] . "  on  ". $row['date_added'];
 
 mysql_close($con);
}
         
function SearchByDate($dateNeeded) 
 {
$con = mysql_connect("localhost","root","");
if(!$con)
{
    die('Could not connect:' . mysql_error());
}
mysql_select_db("db_name",$con);

                $searchdate = mysql_query("Select * 
                From Texts t inner join Members m 
                on  t.author_username = m.username 
                where t.date_added = '$dateNeeded';") or die(mysql_error());;


       While($row = mysql_fetch_assoc($searchdate))
        {
            echo $row['author_username'] . "  wrote  " . $row['text'] . "  on  ". $row['date_added'];
        }
  mysql_close($con); 
 }
 
 

 ?>
  